# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from scrapy.exporters import JsonItemExporter
from mysql import connector


class MovieMysqlPipeline:
    def open_spider(self, spider):
        self.db_cnx = connector.connect(**spider.settings.get("MYSQL_CONFIG"))
        self.db_cursor = self.db_cnx.cursor()

    def process_item(self, item, spider):
        sql = "INSERT INTO movies ( movie_name, movie_rating_number ) VALUES ( %s, %s);"
        data = (item['name'], item['rating_num'])
        self.db_cursor.execute(sql, data)
        return item

    def close_spider(self, spider):
        self.db_cnx.commit()
        self.db_cursor.close()
        self.db_cnx.close()


class MovieJsonPipeline:
    def open_spider(self, spider):
        self.file = open("movie.json", "wb")
        self.exporter = JsonItemExporter(
            self.file, encoding='utf-8', ensure_ascii=False)
        self.exporter.start_exporting()

    def process_item(self, item, spider):
        self.exporter.export_item(item=item)
        return item

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()
